<template>
  <div class="tool-container">
    <el-form class="tool-search" :model="queryParams" ref="queryFormRef" :inline="true">
      <el-form-item prop="scope">
        <DictSelect dict="price_scope" v-model="queryParams.scope" placeholder="类型" clearable />
      </el-form-item>
      <el-form-item prop="type">
        <DictSelect dict="price_type" v-model="queryParams.priceType" placeholder="配置类型" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery" v-hasPermi="['biz:price:list']">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="tool-handle">
      <el-button type="primary" icon="Plus" @click="handleAdd" v-hasPermi="['biz:price:add']">新增定价</el-button>
    </div>
  </div>
  <div class="tabel-container">
    <vxe-grid ref="tableRef" v-bind="gridOptions">
      <template #scope="{ row }">
        <dict-tag :options="price_scope" :value="row.scope" />
      </template>
      <template #type="{ row }">
        <dict-tag :options="price_type" :value="row.priceType" />
      </template>
      <template #handle="{ row }">
        <el-button link type="primary" @click="handleDetail(row)" v-hasPermi="['biz:price:query']">详情</el-button>
        <el-button link type="primary" @click="handleUpdate(row)" v-hasPermi="['biz:price:edit']">编辑</el-button>
        <el-button link type="danger" @click="handleDelete(row)" v-hasPermi="['biz:price:remove']">删除</el-button>
      </template>
      <template #bottom>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
      </template>
    </vxe-grid>
    <EditModel ref="refEditModel" @success="getList"></EditModel>
    <InfoModel ref="refInfoModel"></InfoModel>
  </div>
</template>

<script setup lang="ts">
import { listPrice, delPrice } from '@/api/operation/price/index';
import { BizPriceVo } from '@/api/operation/price/types';
import { getTableConfig } from './columns';
import EditModel from '../common/price/edit/index.vue';
import InfoModel from '../common/price/info/index.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { price_type, price_scope } = toRefs<any>(proxy?.useDict('price_type', 'price_scope'));
const gridOptions = reactive(getTableConfig());
const data = toRef(gridOptions, 'data');
const loading = toRef(gridOptions, 'loading');

const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const refEditModel = ref();
const refInfoModel = ref();

const queryParams = ref<PageQuery>({
  pageNum: 1,
  pageSize: 20,
  scope: '',
  priceType: ''
});

/** 查询租户列表 */
const getList = async () => {
  loading.value = true;
  const res = await listPrice(queryParams.value);
  data.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 搜索重置操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
/** 新增按钮操作 */
const handleAdd = () => {
  refEditModel.value.open();
};

/** 删除按钮操作 */
const handleDelete = async (row?: BizPriceVo) => {
  const _ids = row?.priceId || '';
  await proxy?.$modal.confirm('是否确认删除"' + row?.name + '"的数据项？');
  loading.value = true;
  await delPrice(_ids).finally(() => (loading.value = false));
  await getList();
  proxy?.$modal.msgSuccess('删除成功');
};

/** 修改按钮操作 */
const handleUpdate = async (row?: BizPriceVo) => {
  refEditModel.value.openEdit(row);
};
/** 详情按钮操作 */
const handleDetail = async (row?: BizPriceVo) => {
  refInfoModel.value.open(row);
};

onMounted(() => {
  getList();
});
</script>
